<!--二级集团角色管理 companyRoleManager-->
<template>
  <Layout>
    <Form ref="form1" :model="dataToQuery" :label-width="120">
      <Row v-show="!switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="企业名称：" prop="order">
            <cascader-dom class="query-form-el" ref='queryOrgCascader' @setOrgInfo='setQueryCompanyName' :orgTree="true"
                          :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label="姓名：" >
            <Input  class=''  type='text' v-model='dataToQuery.userName'></Input>
          </FormItem>
        </Col>
        <Col span="8" >
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="企业名称：" prop="belongOrgId">
            <cascader-dom class="query-form-el" ref='queryOrgCascader1' @setOrgInfo='setQueryCompanyName'
                          :orgTree="true"
                          :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label='处罚类别：' prop='punishmentType'>
            <Select class='query-form-el' placeholder="请选择处罚类别" type='text' v-model='dataToQuery.punishmentTypeArrays'
                    multiple>
              <Option v-for="item in dictMap['ENEVT-000002']" :value="item.codingCodeId" :key="item.codingCodeId">
                {{ item.codingName }}
              </Option>
            </Select>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label="事件发生时间：" prop="content">
            <DatePicker class="query-form-el" v-model="dataToQuery.dateRange" format="yyyy-MM-dd" type="daterange"
                        placement="bottom-end" placeholder="请选择查询时间段"
                        @on-change="(data)=> {this.onSelectFieldQueryDateRange(data, 'eventDate')}"></DatePicker>
          </FormItem>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="填表时间：" prop="content">
            <DatePicker class="query-form-el" v-model="dataToQuery.dateRange" format="yyyy-MM-dd" type="daterange"
                        placement="bottom-end" placeholder="请选择查询时间段"
                        @on-change="onSelectQueryDateRange"></DatePicker>
          </FormItem>
        </Col>
        <Col span="8" offset="8">
          <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row class="query-form">
        <Col span="24">
          <Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>
        </Col>
      </Row>
    </Form>
    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" @on-selection-change='getSelectRows'
           :loading='pageStatus.loading'
           border :columns="columns1" :data="dataTable">
    </Table>
    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
    <user-manager-select @closeModel="closeModel"
                         v-if="userForSelectModal"
                         :company-id="currentUser.companyId"
                         :userForSelectModal="userForSelectModal"
                         :model="true" @getSelectModelData="getSelectModelData"/>

  </Layout>
</template>

<script>
import {Api} from '@/api/index.js'
import userManagerSelect from "@/views/base/authorityManager/userManagerSelect";
import pageCommonFunction from '@/components/pageCommonFunction.js'
import {sessionStorage} from "@/common/cacheUtils";

var cascaderDom = () => import('@/components/cascader-dom.vue');

export default {
  components: {
    cascaderDom,userManagerSelect
  },
  data() {
    return {
      ...this.createCommonDataField(),
      data:this.$route.query,
      userForSelectModal:false,
      columns1: [
        {title: '序号', width: 70, fixed: 'left', align: 'center', render: this.indexColumnsRender},
        {key: 'userName', title: '姓名', align: 'center', width: 100},
        {key: 'loginId',  title: '登陆账号', align: 'center', width: 120},
        {key: 'twoLevelOrgname', title: '所属二级集团', align: 'center', minWidth: 160},
        {key: 'companyName', title: '所属单位', align: 'center', minWidth: 160},
        {key: 'orgName', title: '所属部门', align: 'center', width: 160},
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 160,
          align: 'center',
          render: (h, params) => {
            if (this.currentUser.loginId === this.Common.admin || this.currentUser.userId === params.row.createUserId || this.currentUser.userRole.find(obj => obj.roleId == '73b38b37dff64392ba0ee7dd579bf293') != undefined || this.currentUser.userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined) {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: 'red'},
                  on: {
                    click: () => {
                      this.deleteData(params);
                    }
                  }
                }, '删除')
              ]);
            } else {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '删除')
              ]);
            }
          }
        }
      ],
      currentUser:sessionStorage('userId').getObject('userId'),
      tableHeight: 10,
      uploadListVisible: [],
      uploadListPercent: [],
      dictMap: {},
      dataToView: {},
      uploadList: [],
      // api
      queryUrl: Api.queryCompanyRoleManager,
      deleteUrl: Api.deleteCompanyRoleManager,
      updateUrl: Api.updateCompanyRoleManager,
      saveUrl: Api.saveCompanyRoleManager,
    }
  },
  async mounted() {
    this.setTableHeight();
    this.query();
  },
  methods: {
    ...pageCommonFunction,
    deleteData(params) {
      this.deleteDateAjax(params);
    },
    query() {
      if (this.dataToQuery.punishmentTypeArrays) {
        this.dataToQuery.punishmentTypes = this.arrayToInFormat(this.dataToQuery.punishmentTypeArrays)
      }
      this.dataToQuery.roleType = '2'
      this.queryAjax();
    },
    submit(data) {
      data.forEach(item =>{

        item["roleType"] = '2';
      })
      this.$http.post(this.saveUrl, data).then((res) => {
        if (res.data.status === 200) {
          this.$Message.success("保存成功");

        }else{
          this.$Message.error(res.data.msg);
        }
        this.query();
      }).catch(e => {
        this.$Message.error("数据获取失败")
      })

    },
    getSelectModelData(data){
      this.submit(data);
    },
    closeModel(){
      this.userForSelectModal = false;
    },
    addInfo() {
      this.userForSelectModal = true;
    },
    get_uuid() {
      let s = [];
      let hexDigits = "0123456789abcdef";
      for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4";
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
      s[8] = s[13] = s[18] = s[23] = "-";

      let uuid = s.join("");
      let reg = new RegExp("-", "g");
      let a = uuid.replace(reg, "");
      return a;
    },
    handleCancel() {
      this.dataToView = {};
      this.dialogShow = false;
      this.query();
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
        }
      }
      return fmt;
    },
    padLeftZero(str) {
      return ('00' + str).substr(str.length);
    },
    createEmptyDataToAdd() {
      return {
        injuredNum: 0,
        deathNum: 0,
        economicLoss: 0,
        fileids: this.get_uuid(),
        belongOrgId: this.currentUser.twoLevelOrgId,
        belongOrgName: this.currentUser.twoLevelOrgName,
        eventOrgId: this.currentUser.companyId,
        eventOrgName: this.currentUser.companyName,
      }
    }
  },
  watch: {},
}
</script>

<style lang='css' scoped>
.rpt-form {
}

/deep/ .rpt-form .query-form-el {
  width: 100%;
}

/deep/ .rpt-form .ivu-form-item {
  margin-bottom: 24px;
}

</style>
